package com.znyj.servlet.task.executor;

import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.constant.SysConstant;
import com.znyj.servlet.service.ReportService;
import com.znyj.servlet.service.ScheduledService;
import com.znyj.servlet.task.ScheduledTaskMapping;
import com.znyj.servlet.task.model.ScheduledTaskModel;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * Created by gaoliwei02 on 17/6/3.
 */
@Component
public class ScheduledTaskExecutor {
    private static final Logger logger = Logger.getLogger(ScheduledTaskExecutor.class);

    @Resource
    private ReportService reportService;

    @Resource
    private ScheduledService scheduledService;

    @Resource
    private ScheduledTaskMapping scheduledTaskMapping;

    @Async
    public void send(ScheduledTaskModel scheduledTaskModel) {

        long nowOrderId = reportService.getChannalAOrderId(scheduledTaskModel.scheduledEntity.getDeviceId());
        //对比orderId是否实时的相同
        if (nowOrderId == scheduledTaskModel.scheduledEntity.getOpenOrderId()) {
            //第一次发送
            if (scheduledTaskModel.scheduledEntity.getExtraDelay() == 0) {
                JSONObject jsonObject = scheduledTaskModel.send();
                logger.info("scheduled first send-> " +
                        " openOrderId:{}" + scheduledTaskModel.scheduledEntity.getOpenOrderId() +
                        " deviceId:{}" + scheduledTaskModel.scheduledEntity.getDeviceId());
                scheduledTaskModel.scheduledEntity.setExtraDelay(SysConstant.TASK_DELAY);
            }
            //第二次发送
            else if (scheduledTaskModel.scheduledEntity.getExtraDelay() > 0 &&
                    scheduledTaskModel.scheduledEntity.getExtraDelay() < SysConstant.TASK_DELAY * 4) {
                long order = reportService.getChannalAOrderId(scheduledTaskModel.scheduledEntity.getDeviceId());
                //上一次的数据更新成功 更新DB和删除MAP
                if (order == 0) {
                    scheduledService.updateExecuteResult("close finish", scheduledTaskModel.scheduledEntity.getOpenOrderId());
                    scheduledTaskMapping.remove(scheduledTaskModel.scheduledEntity.getOpenOrderId());
                    logger.info("scheduled success-> " +
                            " extraDelay:{}" + scheduledTaskModel.scheduledEntity.getExtraDelay() +
                            " openOrderId:{}" + scheduledTaskModel.scheduledEntity.getOpenOrderId() +
                            " deviceId:{}" + scheduledTaskModel.scheduledEntity.getDeviceId());
                }
                //这次再发
                else {
                    JSONObject jsonObject = scheduledTaskModel.send();
                    logger.info("scheduled continue send-> " +
                            " openOrderId:{}" + scheduledTaskModel.scheduledEntity.getOpenOrderId() +
                            " deviceId:{}" + scheduledTaskModel.scheduledEntity.getDeviceId());
                    long now = scheduledTaskModel.scheduledEntity.getExtraDelay();
                    scheduledTaskModel.scheduledEntity.setExtraDelay(now + SysConstant.TASK_DELAY);
                }
            }
            //最后一次 完全失败，清除所有信息
            else {
                //更新DB和删除MAP
                scheduledService.updateExecuteResult("close error finish", scheduledTaskModel.scheduledEntity.getOpenOrderId());
                scheduledTaskMapping.remove(scheduledTaskModel.scheduledEntity.getOpenOrderId());

                reportService.updateABInfo(
                        scheduledTaskModel.scheduledEntity.getDeviceId(),
                        new Timestamp(SysConstant.DEFAULT_TIME),
                        0,
                        "",
                        0,
                        new Timestamp(SysConstant.DEFAULT_TIME),
                        0,
                        "",
                        0);
                logger.info("scheduled close error oid-> nowOrderId:{}" + nowOrderId +
                        " extraDelay:{}" + scheduledTaskModel.scheduledEntity.getExtraDelay() +
                        " openOrderId:{}" + scheduledTaskModel.scheduledEntity.getOpenOrderId() +
                        " deviceId:{}" + scheduledTaskModel.scheduledEntity.getDeviceId());
            }
        }
        //不一致：已经一下open或者已经close
        else if(nowOrderId == 0){
            //更新DB和删除MAP
            scheduledService.updateExecuteResult("already close:", scheduledTaskModel.scheduledEntity.getOpenOrderId());
            scheduledTaskMapping.remove(scheduledTaskModel.scheduledEntity.getOpenOrderId());
            logger.info("scheduled close error oid-> nowOrderId:{}" + nowOrderId +
                    " openOrderId:{}" + scheduledTaskModel.scheduledEntity.getOpenOrderId() +
                    " deviceId:{}" + scheduledTaskModel.scheduledEntity.getDeviceId());
        }else{
            //更新DB和删除MAP
            scheduledService.updateExecuteResult("close conflict oid:" + nowOrderId, scheduledTaskModel.scheduledEntity.getOpenOrderId());
            scheduledTaskMapping.remove(scheduledTaskModel.scheduledEntity.getOpenOrderId());
            logger.info("scheduled close error oid-> nowOrderId:{}" + nowOrderId +
                    " openOrderId:{}" + scheduledTaskModel.scheduledEntity.getOpenOrderId() +
                    " deviceId:{}" + scheduledTaskModel.scheduledEntity.getDeviceId());
        }
    }

//    @Async
//    public void insert(ScheduledTaskModel scheduledTaskModel) {
//        //插入DB和MAP
//        scheduledService.insertScheduled(scheduledTaskModel.scheduledEntity);
//        scheduledTaskMapping.add(scheduledTaskModel.scheduledEntity.getOpenOrderId(), scheduledTaskModel);
//    }
}
